#include <iostream>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <unistd.h>
#define Path "."
int main()
{
    key_t key = ftok(Path, 100);//不要用小的数字，因为它会和其他进程的key冲突
    int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
    if (shmid == -1)
    {
        perror("shmget:");
    }
    void *shm_ptr = shmat(shmid, NULL, SHM_RND); 
    if (shm_ptr == (void *)-1)
    {
        perror("shmat:");
    }
    char* p=(char*)shm_ptr;
    for(int i=0;i<10;i++){
        p[i]='a'+i;
        sleep(1);
    }
    shmdt(shm_ptr);                          
    if (shmctl(shmid, IPC_RMID, NULL) == -1)
    {
        perror("shmctl:");
    }
    return 0;
}
